import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
import routes from './routes'
import axios from "axios";
import 'font-awesome/css/font-awesome.min.css'
//地图api
import BaiduMap from 'vue-baidu-map'

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)
Vue.use(axios)
Vue.use(BaiduMap,{ak:'fFPXZ1rQCgNZmwnpsEzWkCDYmETrHZvn'})


//NProgress.configure({ showSpinner: false });
axios.defaults.baseURL = '/agent'
// axios.defaults.baseURL = 'http://212.50.245.251:9200'
//全局axios配置
Vue.prototype.$http = axios

const router = new VueRouter({
  routes
})

/*axios请求拦截器--在里面配置,在请求头里面携带token
axios响应拦截器--在里面做配置,如果登录过期,跳转到登录界面*/
//不返回config请求就被拦截了

axios.interceptors.request.use(config=>{
  let token = localStorage.getItem("token")
  //判断token如果不为Long把token加在请求头
  //拼token否则后端拦截器没法做
  if(token){
    config.headers.token=token
  }
  return config
},error=>{
  return Promise.reject(error)
})

//处理后端拦截器返回给路由的信息
axios.interceptors.response.use(resp=>{
  let {success,msg} = resp.data
  if(!success && msg === "noLogin"){
    router.push({path:'/login'})
  }
   if(!success && msg === "noPermission"){
     router.push('/noPermission')
   }
  return resp

},error => {
  return Promise.reject(error)
})

// axios前置拦截器
// to:route路由对象,即将要进入的目标
// from:route路由对象,要离开的路由
// next:next函数,调用方法
// router.beforeEach((to, from, next) => {
//   //NProgress.start();
//
//
//   if (to.path === '/login') {
//     //localStorage存储键值对,保存数据--浏览器保存数据--永久--只能主动移除
//     //sessionStorage关闭浏览器数据就消失
//     //从本地把user拿出来
//     localStorage.removeItem('employee');
//
//   }
//
//   let user = JSON.parse(localStorage.getItem('employee'));
//   if (!user && to.path != '/login') {
//     next({ path:'/login' })
//   } else {
//     //next()意思是直接放行
//     next()
//   }
// })

//router.afterEach(transition => {
//NProgress.done();
//});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

